<?php

namespace app\common\model;

use think\Model;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 数据层模型
 */
class Goodproject extends Model {
    
public $page_info;
    /**
     * 添加品牌
     * @access public
     * @author csdeshang 
     * @param array $data 参数内容
     * @return boolean
     */
    public function addBrand($data) {
        return db('goodsproject')->insertGetId($data);
    }
    
    /**
     * 编辑品牌
     * @access public
     * @author csdeshang 
     * @param array $condition 检索条件
     * @param array $update 更新数据
     * @return boolean
     */
    public function editBrand($condition, $update) {
        return db('goodsproject')->where($condition)->update($update);
    }
    
    /**
     * 删除品牌
     * @access public
     * @author csdeshang
     * @param array $condition 检索条件
     * @return boolean
     */
    public function delBrand($condition) {
        
        return db('goodsproject')->where($condition)->delete();
    }
    
    /**
     * 查询品牌数量
     * @access public
     * @author csdeshang
     * @param array $condition 检索条件
     * @return array
     */
    public function getBrandCount($condition) {
        return db('brand')->where($condition)->count();
    }
    
    /**
     * 品牌列表
     * @access public
     * @author csdeshang 
     * @param array $condition 检索条件
     * @param str $field 字段
     * @param int $page 分页信息
     * @param str $order 排序
     * @return array
     */
    public function getBrandList($condition, $field = '*', $page = 0, $order = 'project_sort asc, project_id desc') {
        if($page) {
            $res= db('goodsproject')->alias('g')
                ->join('goodsproject_class c','g.project_class=c.id')
                ->where($condition)->field('g.*,c.name')->order($order)->paginate($page,false,['query' => request()->param()]);
            $this->page_info=$res;
            return $res->items();
        }else{
            return db('goodsproject')->where($condition)->field($field)->order($order)->select();
        }
    }
    
    /**
     * 通过的品牌列表
     * @access public
     * @author csdeshang 
     * @param array $condition 检索条件
     * @param str $field 字段
     * @param int $page 分页信息
     * @param str $order 排序
     * @return array
     */
    public function getBrandPassedList($condition, $field = '*', $page = 0, $order = 'brand_sort asc, brand_id desc') {
        $condition['brand_apply'] = 1;
        return $this->getBrandList($condition, $field, $page, $order);
    }
    
    /**
     * 未通过的品牌列表
     * @access public
     * @author csdeshang 
     * @param array $condition 检索条件
     * @param string $field 字段
     * @param string $page 分页信息
     * @return array
     */
    public function getBrandNoPassedList($condition, $field = '*', $page = 0) {
        $condition['brand_apply'] = 0;
        return $this->getBrandList($condition, $field, $page);
    }
    
    /**
     * 取单个品牌内容
     * @access public
     * @author csdeshang 
     * @param array $condition 检索条件
     * @param string $field 字段
     * @return array
     */
    public function getBrandInfo($condition, $field = '*') {
        return db('goodsproject')->field($field)->where($condition)->find();
    }

    /**
     * 获取已经审核完成的商品
     * @access public
     * @author csdeshang 
     * @param array $condition 检索条件
     * @param string $field 字段
     * @return array
     */
    public function getGoods($field = '*', $order = 'goods_commonid desc') {
        $where = [
           'goods_state' => 1,
           'goods_verify' => 1,
           'goods_lock' => 0,
           'goods_status' => 1
        ];
        return ;
        
    }

    //获取项目定制列表
    public function getProjectListClass(){
        $where['status'] = 1;
        $data = db('goodsproject_class')->where($where)->select();
        foreach ($data as $k => $v){
            $result[$k]['id'] = $v['id'];
            $result[$k]['title'] = $v['name'];
        }
        return $result;
    }

    //获取项目定制列表
    public function getProjectList($project_title,$project_class){
        if(!empty($project_title)){
            $where['project_title'] = array('like', '%' . $project_title . '%');
        }
        if(!empty($project_class)){
            $where['project_class'] = $project_class;
        }
        $where['status'] = 1;
        $order = 'create_time asc, sale_num desc';
        // $page = intval(input('page'))?intval(input('page')):1;
        // $pagesize = intval(input('pagesize'))?intval(input('pagesize')):15;
        // $offset = ($page-1)*$pagesize;
        // ->limit($offset,$pagesize)
        $data = db('goodsproject')->where($where)->order($order)->select();

        foreach ($data as $k => $v){
            $data[$k]['project_images'] = config('qiniu_http') .'/' . $v['project_images'];
            $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            $title = db('goodsproject_class')->where(['id'=>$v['project_class']])->find();
            $data[$k]['project_class']= $title['name'];
        }
        return $data;
    }

    //获取项目定制详情
    public function getProjectInfo($project_id,$user_id){
        $where['project_id'] = $project_id;
        $data = db('goodsproject')->where($where)->find();
        if($data['status'] == 0){
            return json_fail_return('该项目已下架！');
        }
        $data['project_images'] = config('qiniu_http') .'/' . $data['project_images'];
        $data['project_videos'] = config('qiniu_http') .'/' . $data['project_videos'];
        $data['default_images'] = config('qiniu_http') .'/' . $data['default_images'];
        $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
        $title = db('goodsproject_class')->where(['id'=>$data['project_class']])->find();
        
        #转义  项目定制描述
        $content = htmlspecialchars_decode($data['science']);
        $new_content = strip_tags($content);
        $data['science'] = $new_content;

        
        $project_describe = htmlspecialchars_decode($data['project_describe']);
        $new_project_describe = strip_tags($project_describe);
        $data['project_describe'] = $new_project_describe;

        $data['project_class']= $title['name'];

        //判断商品是否收藏
        $favorite_info = db('favorites')->where(['fav_id'=>$project_id,'fav_type'=>2,'member_id'=>$user_id])->find();
        if(empty($favorite_info)){
            //收藏表里没数据  0:未收藏 1:已收藏
            $data['is_collection'] = 0;
        }else{
            if($favorite_info['status'] == 0){
               //当商品是收藏时
                $data['is_collection'] = 1;
            }else{
                $data['is_collection'] = 0;
            }
        }

        //浏览记录
        #首先查看记录表里面有没有当前商品的信息
        $record_where = [
            'type' => 2,
            'member_id' => $user_id,
            'goods_id' => $project_id,
        ];
        $record_info = db('record')->where($record_where)->find();
        if(!empty($record_info)){
            #如果有记录，更新时间和浏览次数
            $new_record['browse_nums'] = $record_info['browse_nums'] + 1;
            $new_record['add_time'] = time();
            db('record')->where($record_where)->update($new_record);
        }else{
            #没有记录新增
            $record_data = [
                'type' => 2,
                'member_id' => $user_id,
                'goods_id' => $project_id,
                'goods_name' => $data['project_title'],
                'goods_price' => $data['total_price'],
                'goods_images' => $data['project_images'],
                'add_time' => time()
            ];
            db('record')->insertGetId($record_data);
        }
        return $data;
    }

    //项目定制收藏
    public function getCollection($store_id,$status){
        $where['id'] = $store_id;
        $res['status'] = $status == 1 ? 0 : 1;
        $data = db('goodsproject')->where($where)->update($res);
        return $data;
    }
}
?>
